/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package xml.servlet;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.apache.avalon.framework.logger.ConsoleLogger;
import org.apache.avalon.framework.logger.Logger;
import org.apache.fop.apps.Driver;
import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
import xml.dao.AccountDAO;
import xml.dao.EmployerDAO;
import xml.dao.JobDAO;
import xml.dao.JobseekerDAO;
import xml.entity.*;
import xml.util.SupportUtil;

/**
 *
 * @author Kyros
 */
public class Controller extends HttpServlet {

    /**
     * Processes requests for both HTTP
     * <code>GET</code> and
     * <code>POST</code> methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
//        PrintWriter out = response.getWriter();
        HttpSession session = request.getSession(true);
        SupportUtil util = new SupportUtil();
        String urlDispatcher = "";
        request.setCharacterEncoding("UTF-8");

        try {
            String action = request.getParameter("action");
            if (action.equals("Login")) {
                String username = request.getParameter("username");
                String password = request.getParameter("password");
                Account userAccount = util.authenticate(username, password);
                if (userAccount != null) {
                    int type = userAccount.getType();
                    session.setAttribute("TYPE", type);
                    session.setAttribute("USERNAME", username);
                    String xmlPath = getServletContext().getRealPath("/") + "xml/myJobOffers.xml";
                    String xmlPath2 = getServletContext().getRealPath("/") + "xml/currentEmp.xml";
                    String xmlPath3 = getServletContext().getRealPath("/") + "xml/currentJS.xml";
                    File f1 = new File(xmlPath);
                    File f2 = new File(xmlPath2);
                    File f3 = new File(xmlPath3);
                    f1.delete();
                    f2.delete();
                    f3.delete();
                    if (type == 1) {
                        urlDispatcher = "employerPage.jsp";
                    } else if (type == 0) {
                        urlDispatcher = "jobseekerPage.jsp";
                    }
                } else {
                    request.setAttribute("FLAG", "invalid");
                    urlDispatcher = "login.jsp";
                }
            } else if (action.equals("RegisterEmployer")) {
                String username = request.getParameter("username");
                String password = request.getParameter("password");
                String companyName = request.getParameter("companyName");
                String companyDesc = request.getParameter("companyDesc");
                String companyAddress = request.getParameter("companyAddress");
                String city = request.getParameter("city");
                String website = request.getParameter("website");
                String fullname = request.getParameter("fullname");
                String currentPos = request.getParameter("currentPos");
                String phone = request.getParameter("phone");
                String email = request.getParameter("email");
                String address = request.getParameter("address");
                int type = Integer.parseInt(request.getParameter("type"));

                Account account = new Account(username, password, type);
                AccountDAO accountDao = new AccountDAO();
                accountDao.insertAccount(account);

                EmployerDAO employerDao = new EmployerDAO();
                int nextId = employerDao.getNextEmployerId();
                Employer employer = new Employer();

                employer.setAccountUsername(username);
                employer.setAddress(address);
                employer.setCity(city);
                employer.setCompanyAddress(companyAddress);
                employer.setCompanyDesc(companyDesc);
                employer.setCompanyName(companyName);
                employer.setCurrentPos(currentPos);
                employer.setEmail(email);
                employer.setPhone(phone);
                employer.setEmployerId(nextId);
                employer.setFullname(fullname);
                employer.setWebsite(website);

                employerDao.insertEmployer(employer);
                urlDispatcher = "success.jsp";
            } else if (action.equals("RegisterJobSeeker")) {
                String username = request.getParameter("username");
                String password = request.getParameter("password");
                int type = Integer.parseInt(request.getParameter("type"));
                String fullname = request.getParameter("fullname");
                String DOB = request.getParameter("dayOfBirth");
                String MOB = request.getParameter("monthOfBirth");
                String YOB = request.getParameter("yearOfBirth");
                String gender = request.getParameter("gender");
                String marriageStatus = request.getParameter("marriageStatus");
                String address = request.getParameter("address");
                String city = request.getParameter("city");
                String phone = request.getParameter("phone");

                Account account = new Account(username, password, type);
                AccountDAO accountDao = new AccountDAO();
                accountDao.insertAccount(account);

                JobseekerDAO jobseekerDao = new JobseekerDAO();
                int nextId = jobseekerDao.getNextJobseekerId();
                Jobseeker jobseeker = new Jobseeker();

                jobseeker.setJobSeekerId(nextId);
                jobseeker.setAccountUsername(username);
                jobseeker.setFullname(fullname);
                if (!util.isNull(DOB)) {
                    jobseeker.setDayOfBirth(Integer.parseInt(DOB));
                }
                if (!util.isNull(MOB)) {
                    jobseeker.setMonthOfBirth(Integer.parseInt(MOB));
                }
                if (!util.isNull(YOB)) {
                    jobseeker.setYearOfBirth(Integer.parseInt(YOB));
                }
                jobseeker.setGender((short) Integer.parseInt(gender));
                jobseeker.setMarriageStatus((short) Integer.parseInt(marriageStatus));
                jobseeker.setAddress(address);
                jobseeker.setCity(city);
                jobseeker.setPhone(phone);

                jobseekerDao.insertJobseeker(jobseeker);
                urlDispatcher = "success.jsp";
            } else if (action.equals("PostJob")) {
                String jobName = request.getParameter("jobName");
                String Position = request.getParameter("Position");
                String Career = request.getParameter("Career");

                short jobType = (short) Integer.parseInt(request.getParameter("jobType"));
                int quantity = Integer.parseInt(request.getParameter("quantity"));

                String workPlace = request.getParameter("workPlace");
                String salary = request.getParameter("salary");
                String jobDesc = request.getParameter("jobDesc");
                String benefit = request.getParameter("benefit");
                String requiredExp = request.getParameter("requiredExp");
                String requiredEdu = request.getParameter("requiredEdu");
                String otherReq = request.getParameter("otherReq");
                String profile = request.getParameter("profile");

                int dayExpired = Integer.parseInt(request.getParameter("dayExpired"));
                int monthExpired = Integer.parseInt(request.getParameter("monthExpired"));
                int yearExpired = Integer.parseInt(request.getParameter("yearExpired"));

                JobDAO jobDao = new JobDAO();
                int nextId = jobDao.getNextJobId();
                EmployerDAO employerDao = new EmployerDAO();
                String user = session.getAttribute("USERNAME").toString();
                int employerId = employerDao.getEmployerIdByUsername(user);
                Job job = new Job();

                job.setJobId(nextId);
                job.setEmployerId(employerId);
                job.setEmployeeId(1);
                job.setJobName(jobName);
                job.setNeededPos(Position);
                job.setCareer(Career);
                job.setJobType(jobType);
                job.setJobLimit(quantity);
                job.setCity(workPlace);
                job.setSalaryOffer(salary);
                job.setJobDesc(jobDesc);
                job.setJobBenefit(benefit);
                job.setRequiredExp(requiredExp);
                job.setRequiredEdu(requiredEdu);
                job.setOtherRequire(otherReq);
                job.setDayOfProfileDealine(dayExpired);
                job.setMonthOfProfileDealine(monthExpired);
                job.setYearOfProfileDeadline(yearExpired);
                job.setProfileRequire(profile);

                DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
                DocumentBuilder db = dbf.newDocumentBuilder();
                String path = getServletContext().getRealPath("/");
                String xmlPath = path + "xml/myJobOffers.xml";                
                File file = new File(xmlPath);
                boolean exists = file.exists();
                if (exists) {
                    Document doc = db.parse(new File(xmlPath));
                    util.addJobOffersXML(doc, job, xmlPath);
                }
                jobDao.insertJob(job);
                urlDispatcher = "employerPage.jsp";

            } else if (action.equals("ViewMyJobOffer")) {
                String xmlPath = getServletContext().getRealPath("/") + "xml/myJobOffers.xml";
                File file = new File(xmlPath);
                boolean exists = file.exists();
                Jobs jobs = new Jobs();
                String user = session.getAttribute("USERNAME").toString();
                EmployerDAO employerDao = new EmployerDAO();
                int employerId = employerDao.getEmployerIdByUsername(user);
                if (!exists) {
                    JobDAO jobDao = new JobDAO();
                    jobs = jobDao.getEmployerAllJob(user);
                    util.marshal(xmlPath, jobs);
                }
//                DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
//                DocumentBuilder db = dbf.newDocumentBuilder();
//                String path = getServletContext().getRealPath("/");
//                String filePath = path + "xml/myJobOffers.xml";
//                Document doc = db.parse(new File(xmlPath));
//                Jobs jobs2 = util.viewJobOffers(doc);
                Jobs jobs2 = (Jobs) util.unmarshal("xml.entity", xmlPath);
                try {
                    if (jobs2.getJob().get(0).getEmployerId() == employerId) {
                        request.setAttribute("INFO", jobs2.getJob());
                    } else {
                        JobDAO jobDao = new JobDAO();
                        jobs = jobDao.getEmployerAllJob(user);
                        util.marshal(xmlPath, jobs);
                        jobs2 = (Jobs) util.unmarshal("xml.entity", xmlPath);
                        request.setAttribute("INFO", jobs2.getJob());
                    }
                } catch (Exception e) {
                    File fd = new File(xmlPath);
                    fd.delete();
                    JobDAO jobDao = new JobDAO();
                    jobs = jobDao.getEmployerAllJob(user);
                    util.marshal(xmlPath, jobs);
                    jobs2 = (Jobs) util.unmarshal("xml.entity", xmlPath);
                    request.setAttribute("INFO", jobs2.getJob());
                    RequestDispatcher rd = request.getRequestDispatcher("viewJobs.jsp");
                    rd.forward(request, response);
                }
                urlDispatcher = "viewJobs.jsp";
            } else if (action.equals("edit")) {
                int jobId = Integer.parseInt(request.getParameter("jobId"));
//                DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
//                DocumentBuilder db = dbf.newDocumentBuilder();
                String path = getServletContext().getRealPath("/");
                String xmlPath = path + "xml/myJobOffers.xml";
//                Document doc = db.parse(new File(xmlPath));
//                Jobs jobs = util.viewJobOffers(doc);
                Jobs jobs = (Jobs) util.unmarshal("xml.entity", xmlPath);
                Job jobEditting = new Job();
                for (int i = 0; i < jobs.getJob().size(); i++) {
                    if (jobs.getJob().get(i).getJobId() == jobId) {
                        jobEditting = jobs.getJob().get(i);
                    }
                }
                session.setAttribute("AJOB", jobEditting);
                urlDispatcher = "editAJobDetail.jsp";
            } else if (action.equals("Submit")) {

                String jobName = request.getParameter("jobName");
                String neededPos = request.getParameter("neededPos");
                String career = request.getParameter("career");

                short jobType = (short) Integer.parseInt(request.getParameter("jobType"));
                int jobLimit = Integer.parseInt(request.getParameter("jobLimit"));

                String city = request.getParameter("city");
                String salaryOffer = request.getParameter("salaryOffer");
                String jobDesc = request.getParameter("jobDesc");
                String jobBenefit = request.getParameter("jobBenefit");
                String requiredExp = request.getParameter("requiredExp");
                String requiredEdu = request.getParameter("requiredEdu");
                String otherRequire = request.getParameter("otherRequire");
                String profileRequire = request.getParameter("profileRequire");

                int dayExpired = Integer.parseInt(request.getParameter("dayOfProfileDealine"));
                int monthExpired = Integer.parseInt(request.getParameter("monthOfProfileDealine"));
                int yearExpired = Integer.parseInt(request.getParameter("yearOfProfileDeadline"));

                int employerId = Integer.parseInt(request.getParameter("employerId"));
                int employeeId = Integer.parseInt(request.getParameter("employeeId"));
                int jobId = Integer.parseInt(request.getParameter("jobId"));
                Job job = new Job();

                job.setJobId(jobId);
                job.setEmployerId(employerId);
                job.setEmployeeId(1);
                job.setJobName(jobName);
                job.setNeededPos(neededPos);
                job.setCareer(career);
                job.setJobType(jobType);
                job.setJobLimit(jobLimit);
                job.setCity(city);
                job.setSalaryOffer(salaryOffer);
                job.setJobDesc(jobDesc);
                job.setJobBenefit(jobBenefit);
                job.setRequiredExp(requiredExp);
                job.setRequiredEdu(requiredEdu);
                job.setOtherRequire(otherRequire);
                job.setDayOfProfileDealine(dayExpired);
                job.setMonthOfProfileDealine(monthExpired);
                job.setYearOfProfileDeadline(yearExpired);
                job.setProfileRequire(profileRequire);
                JobDAO jobDao = new JobDAO();
                DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
                DocumentBuilder db = dbf.newDocumentBuilder();
                String path = getServletContext().getRealPath("/");
                String xmlPath = path + "xml/myJobOffers.xml";
                Document doc = db.parse(new File(xmlPath));

                util.editJobOffersXML(doc, job, jobId, xmlPath);
                jobDao.updateJob(job, jobId);

                urlDispatcher = "Controller?action=ViewMyJobOffer";
            } else if (action.equals("Cancel")) {
                urlDispatcher = "Controller?action=ViewMyJobOffer";
            } else if (action.equals("AddNewJob")) {
                urlDispatcher = "addJob.jsp";
            } else if (action.equals("delete")) {
                int jobId = Integer.parseInt(request.getParameter("jobId"));
                DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
                DocumentBuilder db = dbf.newDocumentBuilder();
                String path = getServletContext().getRealPath("/");
                String xmlPath = path + "xml/myJobOffers.xml";
                Document doc = db.parse(new File(xmlPath));
                util.deleteJobOffersXML(doc, jobId, xmlPath);
                JobDAO jobDao = new JobDAO();
                jobDao.deleteJob(jobId);
                urlDispatcher = "Controller?action=ViewMyJobOffer";
            } else if (action.equals("viewJS")) {
                String xmlPath = getServletContext().getRealPath("/") + "xml/currentJS.xml";
                File file = new File(xmlPath);
                boolean exists = file.exists();
                Jobseeker js = new Jobseeker();
                //   Jobseekers jsList = new Jobseekers();
                if (!exists) {
                    JobseekerDAO jsDao = new JobseekerDAO();
                    String user = session.getAttribute("USERNAME").toString();
                    js = jsDao.getCurrentJobSeeker(user);
                    util.marshal(xmlPath, js);
                }
                Jobseeker result = (Jobseeker) util.unmarshal("xml.entity", xmlPath);
                session.setAttribute("JSINFO", result);
                urlDispatcher = "viewDetailedJS.jsp";
            } else if (action.equals("viewEmp")) {
                String xmlPath = getServletContext().getRealPath("/") + "xml/currentEmp.xml";
                File file = new File(xmlPath);
                boolean exists = file.exists();
                Employer emp = new Employer();
                if (!exists) {
                    EmployerDAO empDao = new EmployerDAO();
                    String user = session.getAttribute("USERNAME").toString();
                    emp = empDao.getCurrentEmployer(user);
                    util.marshal(xmlPath, emp);
                }
                Employer result = (Employer) util.unmarshal("xml.entity", xmlPath);
                session.setAttribute("EmpINFO", result);
                urlDispatcher = "viewDetailedEmp.jsp";
            } else if (action.equals("Search")) {
                String searchName = request.getParameter("searchName");
                String searchCareer = request.getParameter("searchCareer");
                String searchCity = request.getParameter("searchCity");
                String path = getServletContext().getRealPath("/");
                String xmlPath = path + "xml/myJobOffers.xml";
                Jobs jobs = (Jobs) util.unmarshal("xml.entity", xmlPath);
                Jobs jobs2 = new Jobs();

                for (int i = 0; i < jobs.getJob().size(); i++) {
                    if ((jobs.getJob().get(i).getJobName().contains(searchName))
                            && (jobs.getJob().get(i).getCareer().contains(searchCareer))
                            && (jobs.getJob().get(i).getCity().contains(searchCity))) {
                        jobs2.getJob().add(jobs.getJob().get(i));
                    }
                }
//                DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
//                DocumentBuilder db = dbf.newDocumentBuilder();
//                Document doc = db.parse(new File(xmlPath));
//                jobs2 = util.searchJobOffersXML(doc, searchName);

                request.setAttribute("INFO", jobs2.getJob());
                urlDispatcher = "viewJobs.jsp";
            } else if (action.equals("ViewAllJobOffer")) {
                String xmlPath = getServletContext().getRealPath("/") + "xml/myJobOffers.xml";
//                DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
//                DocumentBuilder db = dbf.newDocumentBuilder();
//                Document doc = db.parse(new File(xmlPath));
                File file = new File(xmlPath);

//                util.deleteFileXML(doc,xmlPath);
                boolean exists = file.exists();
                Jobs jobs = new Jobs();
                if (!exists) {
                    JobDAO jobDao = new JobDAO();
                    jobs = jobDao.getAllJobs();
                    util.marshal(xmlPath, jobs);
                }
//                String path = getServletContext().getRealPath("/");
//                String filePath = path + "xml/myJobOffers.xml";
//                Jobs jobs2 = util.viewJobOffers(doc);
                Jobs jobs2 = (Jobs) util.unmarshal("xml.entity", xmlPath);
                request.setAttribute("INFO", jobs2.getJob());
                urlDispatcher = "viewJobsJS.jsp";
            } else if (action.equals("Logout")) {
                String xmlPath = getServletContext().getRealPath("/") + "xml/myJobOffers.xml";
                String xmlPath2 = getServletContext().getRealPath("/") + "xml/currentEmp.xml";
                String xmlPath3 = getServletContext().getRealPath("/") + "xml/currentJS.xml";
                File f1 = new File(xmlPath);
                File f2 = new File(xmlPath2);
                File f3 = new File(xmlPath3);
                f1.delete();
                f2.delete();
                f3.delete();
                session.invalidate();
                urlDispatcher = "login.jsp";
            } else if (action.equals("JobSearch")) {
                String searchName = request.getParameter("searchName");
                String searchCareer = request.getParameter("searchCareer");
                String searchCity = request.getParameter("searchCity");
                String path = getServletContext().getRealPath("/");
                String xmlPath = path + "xml/myJobOffers.xml";

                String resultPath = path+ "xml/jobExport.xml";

                        Jobs jobs = (Jobs) util.unmarshal("xml.entity", xmlPath);
                Jobs jobs2 = new Jobs();

//                for (int i = 0; i < jobs.getJob().size(); i++) {
//                    if ((jobs.getJob().get(i).getJobName().contains(searchName))
//                            && (jobs.getJob().get(i).getCareer().toLowerCase().contains(searchCareer))
//                            && (jobs.getJob().get(i).getCity().toLowerCase().contains(searchCity))) {
//                        jobs2.getJob().add(jobs.getJob().get(i));
//                    }
//                }

                DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
                DocumentBuilder db = dbf.newDocumentBuilder();
                Document doc = db.parse(new File(xmlPath));
                jobs2 = util.searchJobOffersXML(doc, searchName, searchCareer, searchCity);

                util.marshal(resultPath, jobs2);

                request.setAttribute("INFO", jobs2.getJob());
                urlDispatcher = "viewJobsJS.jsp";
            } else if (action.equals("view")) {
                int jobId = Integer.parseInt(request.getParameter("jobId"));
//                DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
//                DocumentBuilder db = dbf.newDocumentBuilder();
                String path = getServletContext().getRealPath("/");
                String xmlPath = path + "xml/myJobOffers.xml";
//                Document doc = db.parse(new File(xmlPath));
//                Jobs jobs = util.viewJobOffers(doc);
                Jobs jobs = (Jobs) util.unmarshal("xml.entity", xmlPath);
                Job jobEditting = new Job();
                for (int i = 0; i < jobs.getJob().size(); i++) {
                    if (jobs.getJob().get(i).getJobId() == jobId) {
                        jobEditting = jobs.getJob().get(i);
                    }
                }
                session.setAttribute("AJOB", jobEditting);
                urlDispatcher = "viewAJobDetail.jsp";
            } else if (action.equals("JSviewEmp")) {
                String xmlPath = getServletContext().getRealPath("/") + "xml/currentEmp.xml";
       //         System.out.println(request.getParameter("employerId"));
                int employerId = Integer.parseInt(request.getParameter("employerId"));
                File file = new File(xmlPath);
                boolean exists = file.exists();
                Employer emp = new Employer();
                if (!exists) {
                    EmployerDAO empDao = new EmployerDAO();
                    String user = empDao.getUsernameByEmployerId(employerId);
                    emp = empDao.getCurrentEmployer(user);
                    util.marshal(xmlPath, emp);
                }
                Employer result = (Employer) util.unmarshal("xml.entity", xmlPath);
                session.setAttribute("EmpINFO", result);
                urlDispatcher = "JSviewDetailedEmp.jsp";
            } else if (action.equals("Export")){
                String user = session.getAttribute("USERNAME").toString();
                String xmlPath1 = getServletContext().getRealPath("/") + "xml/myJobOffers.xml";
                String xmlPath2 = getServletContext().getRealPath("/") + "xml/jobExport.xml";
                String xslPath = getServletContext().getRealPath("/") + "xsl/viewJobsFO.xsl";
                String foPath = getServletContext().getRealPath("/") + "fo/viewJobsFO.fo";
                String pdfPath = getServletContext().getRealPath("/") + "pdf/"
                        + user + "_JobInformation.pdf";

                File file1 = new File(xmlPath1);
                File file2 = new File(xmlPath2);
                boolean exists1 = file1.exists();
                boolean exists2 = file2.exists();
                if(exists1 && exists2){
                    util.transform(xmlPath2, xslPath, foPath);
                } else {
                    util.transform(xmlPath1, xslPath, foPath);
                }

    //            util.transform(xmlPath, xslPath, foPath);
                util.exportPDF(foPath, pdfPath);
                file2.delete();
                File file = new File(foPath);
                FileInputStream input = new FileInputStream(file);
                ByteArrayOutputStream outt = new ByteArrayOutputStream();
                response.setContentType("application/pdf");
                response.setHeader("Content-Disposition", "attachment;filename="+ user + "_JobInformation.pdf");
                Driver driver = new Driver(new InputSource(input), outt);
                Logger log = new ConsoleLogger(ConsoleLogger.LEVEL_WARN);
                driver.setRenderer(Driver.RENDER_PDF);
                driver.run();
                byte[] content = outt.toByteArray();
                response.setContentLength(content.length);
                
                response.getOutputStream().write(content);
                response.getOutputStream().flush();

           //     urlDispatcher = "Controller?action=ViewAllJobOffer";
            } else if (action.equals("viewAll")){
                int jobId = Integer.parseInt(request.getParameter("jobId"));
                String path = getServletContext().getRealPath("/");
                String xmlPath = path + "xml/myJobOffers.xml";
                String xmlResult1 = path + "xml/viewJob.xml";
                String xmlResult2 = path + "xml/currentEmp.xml";
                Jobs jobs = (Jobs) util.unmarshal("xml.entity", xmlPath);
                Job jobEditting = new Job();
                for (int i = 0; i < jobs.getJob().size(); i++) {
                    if (jobs.getJob().get(i).getJobId() == jobId) {
                        jobEditting = jobs.getJob().get(i);

                    }
                }
                util.marshal(xmlResult1, jobEditting);
                int employerId = jobEditting.getEmployerId();
                File file = new File(xmlPath);
                boolean exists = file.exists();
                Employer emp = new Employer();
                if (exists) {
                    EmployerDAO empDao = new EmployerDAO();
                    String user = empDao.getUsernameByEmployerId(employerId);
                    emp = empDao.getCurrentEmployer(user);
                    util.marshal(xmlResult2, emp);
                }
                urlDispatcher = "viewDetailedJob.jsp";


            }
            RequestDispatcher rd = request.getRequestDispatcher(urlDispatcher);
            rd.forward(request, response);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
//            out.close();
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP
     * <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP
     * <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
